package com.example.demo.his.leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 两数之和
 *
 * @auther zzc
 * @date 2021/2/26 11:09
 */
public class 两素之和 {
    public static void main(String[] args) {
        int nums[] = {1, 2, 5, 7, 10, 19};
        int target = 12;
        int[] ints = twoSumTwo(nums, target);
    }

    public static int[] twoSumOne(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    System.out.println(Arrays.toString(new int[]{i, j}));
                }
            }
        }
        return null;
    }

    public static int[] twoSumTwo(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                System.out.println(Arrays.toString(new int[]{map.get(target - nums[i]), i}));
            }
            map.put(nums[i], i);
        }
        return null;
    }

    public static int[] twoadd(int[] nums, int tar) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(tar - nums[i])) {
                System.out.println(Arrays.toString(new int[]{map.get(tar - nums[i]), i}));

            }
            map.put(nums[i], i);
        }
        return null;
    }
}
